package com.ktjiaoyu.utils;

import java.util.ArrayList;
import java.util.List;

public class Page <T>{
    private Integer pageNo=1;
    private Integer pageSize=5;
    private Integer totalCount=null;
    private Integer totalPageCount=1;

    private List<T> data;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        if (pageNo<=0){
            pageNo=1;
        }
        if (totalCount!=null){
            if (totalPageCount>=1 && pageNo>totalPageCount){
                pageNo=totalPageCount;
            }
        }
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
        this.totalPageCount=this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
    }

    public Integer getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public int offset(){
        return (pageNo-1)*pageSize;
    }

    public boolean isFirst(){
        return pageNo==1;
    }

    public boolean isLast(){
        return pageNo==totalPageCount;
    }

    public boolean hasNext(){
        return pageNo<totalPageCount;
    }

    public boolean hasPrevious(){
        return pageNo>1;
    }

    public List<Integer> getShowPageNos(int showCount){
        List<Integer> list=new ArrayList<Integer>();

        list.add(pageNo);

        int step=1;
        while (list.size()<showCount){
            boolean added=false;
            int left=pageNo-step;
            int right=pageNo+step;
            if (left>=1){
                list.add(0,left);
                added=true;
            }
            if (right<=totalPageCount){
                list.add(right);
                added=true;
            }
            if (!added){
                break;
            }
            step++;
        }
        return list;
    }
}
